from rest_framework.generics import ListAPIView,CreateAPIView,UpdateAPIView,RetrieveAPIView
from rest_framework.response import Response
from apps.questions.models import Label,Question,Reply
from apps.users.models import User
from apps.questions.serializers import LabelListSerializer,LabelUserQuestionListSerializer,QuestionListSerializer,ReplyQuestionSerializer,QuestionDetailsSerializer
from apps.questions.serializers import ReplySerializers,QuestionPublishSerializer



# Create your views here.
#所有标签列表
class LabelListAPIView(ListAPIView):

    def get_queryset(self):
        return Label.objects.all()

    def get_serializer_class(self):
        return LabelListSerializer


#用户关注的标签
class LabelUserListAPIView(ListAPIView):

    def get_queryset(self):
        id = self.request.user.id
        try:
            return User.objects.get(id=id).labels

        except:
            return User.objects.all()

    def get_serializer_class(self):
        return LabelListSerializer


#最新回答的问题
class NewQuestionsAPIView(ListAPIView):

    def get_queryset(self):
        id = int(self.kwargs.get('id'))
        try:
            labels = Label.objects.get(id=id)
            return Question.objects.filter(labels=labels).order_by('-replytime')
        except:
            return Question.objects.all().order_by('-replytime')

    def get_serializer_class(self):
        return QuestionListSerializer




#热门回答的问题
class MostQuestionsAPIView(ListAPIView):

    def get_queryset(self):
        id = int(self.kwargs.get('id'))
        try:
            labels = Label.objects.get(id=id)
            return Question.objects.filter(labels=labels).order_by('-reply')
        except:
            return Question.objects.all().order_by('-reply')

    def get_serializer_class(self):
        return QuestionListSerializer



#等待回答的问题
class WaitQuestionsAPIView(ListAPIView):

    def get_queryset(self):
        id = int(self.kwargs.get('id'))
        try:
            labels = Label.objects.get(id=id)
            return Question.objects.filter(reply=0,labels=labels)
        except:
            return Question.objects.filter(reply=0)

    def get_serializer_class(self):
        return QuestionListSerializer


#发布问题
class PostQuestionsAPIView(CreateAPIView):

    def get_queryset(self):
        return Question.objects.all()

    def get_serializer_class(self):
        return QuestionPublishSerializer

    def create(self, request, *args, **kwargs):
        data = request.data
        data['user'] = request.user.id
        ser = QuestionPublishSerializer(data=data)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response({"message": "ok", "success": True})



#问题详情
class QuestionListAPIView(RetrieveAPIView):

    def get_object(self):
        id = self.kwargs.get('id')
        return Question.objects.get(id=id)

    def get_serializer_class(self):
        return QuestionDetailsSerializer

    def retrieve(self, request, *args, **kwargs):
        question = self.get_object()
        question.visits += 1
        question.save()
        replies = question.replies.all()
        question.comment_question = []
        question.answer_question = []

        for item in replies:
            if item.type == 0:  # 问题的评论
                question.comment_question.append(item)
            elif item.type == 2:  # 回答的评论
                question.answer_question.append(item)

        s = QuestionDetailsSerializer(instance=question)
        return Response(s.data)



#问题有用
class UsefulQuestionsAPIView(UpdateAPIView):

    def get_queryset(self):
        id = self.kwargs.get('id')
        return Question.objects.get(id=id)

    def get_serializer_class(self):
        return QuestionListSerializer

    def update(self, request, *args, **kwargs):
        id = self.kwargs.get('id')
        question = Question.objects.get(id=id)
        question.useful_count += 1
        self.perform_update(question)

        return Response({"message": "ok", "success": True})


#问题没用
class UNUsefulQuestionsAPIView(UpdateAPIView):

    def get_queryset(self):
        id = self.kwargs.get('id')
        return Question.objects.get(id=id)

    def get_serializer_class(self):
        return QuestionListSerializer

    def update(self, request, *args, **kwargs):
        id = self.kwargs.get('id')
        question = Question.objects.get(id=id)
        question.useful_count -= 1
        if question.useful_count < 0:
            return Response({"message": "已经最小了", "success": False})
        else:
            self.perform_update(question)
            return Response({"message": "ok", "success": True})


#回答问题
class ReplyQuestionAPIView(CreateAPIView):

    def get_queryset(self):
        problem = self.kwargs.get('problem')
        return Reply.objects.get(problem=problem)

    def get_serializer_class(self):
        return ReplySerializers

    def create(self, request, *args, **kwargs):
        data = self.request.data
        user = request.user
        data['user'] = user.id
        serializer = ReplySerializers(data=data)
        serializer.is_valid()
        reply = serializer.save()
        problem = reply.problem
        if request.data.get('type') == 2:
            problem.reply += 1
            problem.replyname = reply.user.username
            problem.replytime = reply.createtime
            problem.save()
        return Response({"message": "ok", "success": True})


#回答有用
class ReplyQuestionUsefulAPIView(UpdateAPIView):

    def get_queryset(self):
        id = self.kwargs.get('id')
        return Reply.objects.get(id=id)

    def get_serializer_class(self):
        return ReplySerializers

    def update(self, request, *args, **kwargs):
        id = self.kwargs.get('id')
        reply = Reply.objects.get(id=id)
        reply.useful_count += 1
        reply.save()

        return Response({"message": "ok", "success": True})


#回答没用
class ReplyQuestionUNUsefulAPIView(UpdateAPIView):

    def get_queryset(self):
        id = self.kwargs.get('id')
        return Reply.objects.get(id=id)

    def get_serializer_class(self):
        return ReplySerializers

    def update(self, request, *args, **kwargs):
        id = self.kwargs.get('id')
        reply = Reply.objects.get(id=id)
        reply.useful_count -= 1
        if reply.useful_count < 0:
            return Response({"message": "已经最小了", "success": False})
        else:
            reply.save()
            return Response({"message": "ok", "success": True})


#关注标签
class AttentionLabelAPIView(UpdateAPIView):

    def get_queryset(self):
        id = self.kwargs.get('id')
        return Label.objects.get(id=id)

    def get_serializer_class(self):
        return LabelListSerializer

    def update(self, request, *args, **kwargs):
        label_id = self.kwargs.get('id')
        user_id = self.request.user.id
        label = Label.objects.get(id=label_id)
        label.users.add(user_id)
        label.save()

        return Response({"message": "ok", "success": True})



#取消关注标签
class CancelAttentionLabelAPIView(UpdateAPIView):

    def get_queryset(self):
        id = self.request.user.id
        return Label.objects.get(id=id)

    def get_serializer_class(self):
        return LabelListSerializer

    def update(self, request, *args, **kwargs):
        label_id = self.kwargs.get('id')
        user_id = self.request.user.id
        label = Label.objects.get(id=label_id)
        label.users.remove(user_id)
        label.save()

        return Response({"message": "ok", "success": True})


#更多标签
class LabelsListAPIView(ListAPIView):

    def get_queryset(self):
        return Label.objects.all()

    def get_serializer_class(self):
        return LabelUserQuestionListSerializer


#标签详情
class LabelsDetailsListAPIView(RetrieveAPIView):

    def get_object(self):
        id = self.kwargs.get('id')
        return Label.objects.get(id=id)


    def get_serializer_class(self):
        return LabelUserQuestionListSerializer





